﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;

namespace Light.Framework.Model
{
    /// <summary>
    /// 实体排序对象
    /// </summary>
    public class ModelOrder
    {
        Dictionary<string, bool> dic = new Dictionary<string, bool>();

        public void Add(string field, bool isAscending)
        {
            if (!dic.Keys.Contains(field))
                dic.Add(field, isAscending);
            else
                dic[field] = isAscending;
        }

        public void Clear()
        {
            dic.Clear();
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return dic.Count == 0;
            }
        }

        /// <summary>
        /// 转换成NHibernate的排序数组
        /// </summary>
        public Order[] Orders
        {
            get
            {
                List<Order> orders = new List<Order>();
                foreach (var field in dic.Keys)
                {
                    orders.Add(new Order(field, dic[field]));
                }
                if (orders.Count == 0)
                {
                    orders.Add(new Order("ID", false));
                }
                return orders.ToArray();
            }
        }
    }
}
